package com.hrbxlh.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import com.fasterxml.jackson.core.JsonProcessingException;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.hrbxlh.actionImpl.CourseActionImpl;
import com.hrbxlh.actionImpl.DepActionImpl;

/**
* -------------------------------------------------------
* Copyright (c) 2017, 鑫联华信息科技股份有限公司
* All rights reserved.
* FileName：DepAction.java
* Description：部门action类
* History：
* Date           Author               Desc
* 2017.1.6		  xzx                 建立
* -------------------------------------------------------
*/

@Controller
public class DepAction {
	
	@Autowired
	@Qualifier("depActionImpl")
	private DepActionImpl depActionImpl;

	@Autowired
	@Qualifier("courseActionImpl")
	private CourseActionImpl courseActionImpl;
	
	/**
	 * @param response
	 * @return  添加部门,传回部门名称，父部门id。
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/addDep", method = RequestMethod.POST)
	public void addDep(String jsonStr, HttpServletResponse response) {
		
		//转换json格式工具
		ObjectMapper mapper = new ObjectMapper();
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();
		//日志信息
		Logger logger = Logger.getLogger(DepAction.class.getName());
		//成功失败标识
		String resultMark = "fail";
		try {
			//获取参数值
			Map<String,Object> paramMap = mapper.readValue(jsonStr, resultMap.getClass());
			if (courseActionImpl.paramCheck(paramMap.get("depName"), response, resultMap, mapper)) return;
			//添加部门
			Map<String,Object> resultFlag = depActionImpl.addDep(paramMap);
			//返回参数
			resultMark = ((Boolean)resultFlag.get("result")) ? "success" : "fail";
			if(resultMark.equals("fail")){
				resultMap.put("failMsg", resultFlag.get("failMsg"));
			}
			resultMap.put("result", resultMark);
			logger.info("添加部门,参数为："+jsonStr);
			courseActionImpl.responseStream(response, mapper.writeValueAsString(resultMap));
		} catch (Exception e) {
			resultMap.put("result", "fail");
			e.printStackTrace();
			logger.error("添加部门出现异常addDep", e);
			try {
				courseActionImpl.responseStream(response, mapper.writeValueAsString(resultMap));
			} catch (JsonProcessingException e1) {
				e1.printStackTrace();
			}
		}
	}
	
	/**
	 * @param response
	 * @return  获取修改部门信息，修改部门页面,需要部门id，传回该部门信息，以及全部部门信息。
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/skipSaveDep", method = RequestMethod.POST)
	public void skipSaveDep(String jsonStr, HttpServletResponse response) {
		
		//转换json格式工具
		ObjectMapper mapper = new ObjectMapper();
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();
		//日志信息
		Logger logger = Logger.getLogger(DepAction.class.getName());
		try {
			//获取页面参数值
			Map<String,Object> preParamMap = mapper.readValue(jsonStr, resultMap.getClass());
			//获取该部门信息
			List<Map<String, Object>> depMessageList = depActionImpl.getDepList(preParamMap);
			if(depMessageList != null && depMessageList.size() != 0){
				resultMap.put("dep", depMessageList.get(0));
			}
		} catch (Exception e) {
			resultMap.put("failMsg", "跳转页面失败");
			e.printStackTrace();
			logger.error("跳转修改部门页面出现异常skipSaveDep", e);
		} finally {
			try {
				courseActionImpl.responseStream(response,  mapper.writeValueAsString(resultMap));
			} catch (JsonProcessingException e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * @description   修改部门,传回部门id，部门名称，父部门id
	 * @param response  
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/saveDep", method = RequestMethod.POST)
	public void saveDep(String jsonStr, HttpServletResponse response)  {
		
		//转换json格式工具
		ObjectMapper mapper = new ObjectMapper();
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();
		//日志信息
		Logger logger = Logger.getLogger(DepAction.class.getName());
		//成功失败标识
		String resultMark = "fail";
		try {
			//获取参数值
			Map<String,Object> paramMap = mapper.readValue(jsonStr, resultMap.getClass());
			if (courseActionImpl.paramCheck(paramMap.get("depName"), response, resultMap, mapper)) return;
            //修改部门
			Map<String, Object> flagMap = depActionImpl.saveDep(paramMap);
			resultMark = ((Boolean)flagMap.get("result") == true) ? "success" : "fail";
			resultMap.put("result", resultMark);
			if(resultMark.equals("fail")){
				resultMap.put("failMsg", flagMap.get("failMsg"));
			}
			logger.info("修改部门参数为："+jsonStr);
			courseActionImpl.responseStream(response,  mapper.writeValueAsString(resultMap));
		} catch (Exception e) {
			resultMap.put("result", "fail");
			e.printStackTrace();
			logger.error("修改部门出现异常saveDep", e);
			try {
				courseActionImpl.responseStream(response,  mapper.writeValueAsString(resultMap));
			} catch (JsonProcessingException e1) {
				e1.printStackTrace();
			}
		}
	}
	
	/**
	 * @param response
	 * @param jsonStr 参数：人员id集合。List<String>形式   jsonStr =["1","2","3"...]
	 * @return  删除部门
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/delDeps", method = RequestMethod.POST)
	public void delDeps(String jsonStr, HttpServletResponse response)  {
		
		//转换json格式工具
		ObjectMapper mapper = new ObjectMapper();
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();
		//日志信息
		Logger logger = Logger.getLogger(DepAction.class.getName());
		//成功失败标识
		String resultMark = "fail";
		//用于转换参数
		List<String> list = new ArrayList<String>();
		try {
			//获取参数值
			List<String> paramList = mapper.readValue(jsonStr, list.getClass());
            //删除部门操作
			Map<String, String> delResult = depActionImpl.delDep(paramList);
			//返回成功失败
			resultMark = (delResult.get("result").equals("true")) ? "success" : "fail";
			resultMap.put("result", resultMark);
			if(resultMark.equals("fail")){
				resultMap.put("failMsg", delResult.get("errorMsg"));
			}
			logger.info("删除部门参数为："+jsonStr);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("删除部门出现异常delDeps", e);
		} finally {
			try {
				courseActionImpl.responseStream(response, mapper.writeValueAsString(resultMap));
			} catch (JsonProcessingException e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * @param  jsonStr:pageNow,pageSize(一般是10)，页数从1开始，第一页pageNow传1
	 * @return  部门列表页面，传回全部部门信息。进页面，翻页，条件查询都是这个。
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/getDepList", method = RequestMethod.POST)
	public void getDepList(String jsonStr, HttpServletResponse response) {
		
		//转换json格式工具
		ObjectMapper mapper = new ObjectMapper();
		//存放返回值
		Map<String, Object> returnMap = new HashMap<String, Object>();
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();
		//日志信息
		Logger logger = Logger.getLogger(DepAction.class.getName());
		try {
			//获取页面参数值
			Map<String, Object> preParamMap = mapper.readValue(jsonStr, returnMap.getClass());
			//获取部门列表信息
			Map<String, Object> resultAllMap = depActionImpl.getDepListByPage(preParamMap);
			if(resultAllMap != null){
				//返回值
				resultMap.put("depList", resultAllMap.get("depList"));
				resultMap.put("totalPage", resultAllMap.get("totalPage"));
				resultMap.put("pageNow", resultAllMap.get("pageNow"));
			}
			resultMap.put("pageSize", preParamMap.get("pageSize"));
			resultMap.put("depName", preParamMap.get("depName"));
			resultMap.put("result", "success");
		} catch (Exception e) {
			resultMap.put("result", "fail");
			resultMap.put("failMsg", "查询部门列表失败");
			e.printStackTrace();
			logger.error("获取部门列表页面出现异常getDepList", e);
		} finally {
			try {
				courseActionImpl.responseStream(response, mapper.writeValueAsString(resultMap));
			} catch (JsonProcessingException e) {
				e.printStackTrace();
			}
		}
	}
}